﻿using QRCoder;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
//因为我们需要调用API函数，所以必须创建System.Runtime.InteropServices 命名空间以提供可用于访问 .NET 中的 COM 对象和本机 API 的类的集合。

using System.Text;
using TPMS_ScanComparison.Helper;
using TPMS_ScanComparison.Model;

namespace IniHelp
{

    public class IniFile
    {

        public string path;    //INI文件名

        [DllImport("kernel32")]

        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //[DllImport("kernel32")]

        //private static extern long WritePrivateProfileint(string section, string key, int val, string filePath);

        //[DllImport("kernel32")]

        //private static extern long WritePrivateProfilelong(string section, string key, long val, string filePath);

        [DllImport("kernel32")]

        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        //声明读写INI文件的API函数
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);


        public IniFile(string INIPath)
        {

            path = INIPath;

        }

        //类的构造函数，传递INI文件名

        public void IniWriteValue(string Section, string Key, string Value)
        {
            if (Value == "") Value = "-1";
            WritePrivateProfileString(Section, Key, Value, this.path);
        }

        public void IniWriteValue(string Section, string Key, int Value)
        {
            WritePrivateProfileString(Section, Key, Value.ToString(), this.path);

        }
      
        //public void IniWriteValuelong(string Section, String Key, long Value)
        //{
        //    WritePrivateProfilelong(Section, Key, Value, this.path);
        //}

        //public void IniWriteValueint(string Section, String Key, int Value)
        //{
        //    WritePrivateProfileint(Section, Key, Value, this.path);
        //}


        //写INI文件



        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            StringBuilder strTemp = new StringBuilder("-1");
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
            //当temp为空时，temp=strTemp （-1）表示无值
            if (temp.Length == 0)
            {
                temp = strTemp;
            }
            if (temp.ToString() == "-1") return "";
            return temp.ToString();
        }

        public int IniReadInt(string Section, string Key, int defaultvalue=-1)
        {
            string tmp = IniReadValue(Section,Key,defaultvalue.ToString());
            try
            {
                return int.Parse(tmp);
            }
            catch
            {
                return defaultvalue;
            }
        }

        public string IniReadValue(string Section, string Key,string defaultvalue)
        {
            StringBuilder temp = new StringBuilder(255);
            StringBuilder strTemp = new StringBuilder("-1");
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
            //当temp为空时，temp=strTemp （-1）表示无值
            if (temp.Length == 0)
            {
                temp = strTemp;
                IniWriteValue(Section, Key, defaultvalue);
                return defaultvalue;
            }
            if (temp.ToString() == "-1" && defaultvalue !="") return defaultvalue;
            if (temp.ToString() == "-1") return "";
            return temp.ToString(); ;
           
        }

        public string IniReadValue(string Section)
        {
            StringBuilder temp = new StringBuilder(255);
            StringBuilder strTemp = new StringBuilder("-1");
            int i = GetPrivateProfileString(Section, "", "", temp, 255, this.path);
            //当temp为空时，temp=strTemp （-1）表示无值
            if (temp.Length == 0)
            {
                temp = strTemp;
            }
            return temp.ToString();
        }



        #region 读取ini文件主键
        public static void ReadSections(string ReadFile_ini, string str, ref List<string> list_checkItems)
        {
            //string ReadFile_ini = "C:\\ParaData\\Cam0\\" + Parameter.strProgram_cam + "\\" + Parameter.strProgram_cam + ".ini";
            //Note:必须得用Bytes来实现，StringBuilder只能取到第一个Section
            byte[] Buffer = new byte[65535];
            int bufLen = 0;
            bufLen = IniHelp.IniFile.GetPrivateProfileString(str, null, null, Buffer,
            Buffer.GetUpperBound(0), ReadFile_ini);
            GetStringsFromBuffer(Buffer, bufLen, ref list_checkItems);
        }

        public static void GetStringsFromBuffer(Byte[] Buffer, int bufLen, ref List<string> list_checkItems)
        {
            list_checkItems.Clear();
            if (bufLen != 0)
            {
                int start = 0;
                for (int i = 0; i < bufLen; i++)
                {
                    if ((Buffer[i] == 0) && ((i - start) > 0))
                    {
                        String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
                        list_checkItems.Add(s);
                        start = i + 1;
                    }
                }
            }
        }
        #endregion

        //读取INI文件指定

    }
}